package com.zhilianbao.leyaogo.ui.activity;

import android.os.AsyncTask;
import android.os.Bundle;

import com.bql.utils.AsyncTaskUtils;
import com.bql.utils.CheckUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.ui.activity.base.BaseOkHttpActivity;
import com.zhilianbao.leyaogo.ui.activity.home.SelectSupplierActivity;
import com.zhilianbao.leyaogo.utils.AddressUtils;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.widgets.CountDownProgress;

import butterknife.BindView;
import butterknife.OnClick;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * ClassName: SplashActivity <br>
 * Description: 启动页面<br>
 * Author: Cyarie <br>
 * Created: 2016/10/28 14:54 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class SplashActivity extends BaseOkHttpActivity {

    @BindView(R.id.iv_advertisement)
    GifImageView mIvAdvertisement;
    @BindView(R.id.countdown_view)
    CountDownProgress mCountdownView;
    private GifDrawable mGifDrawable;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        mGifDrawable = (GifDrawable) mIvAdvertisement.getBackground();
        mGifDrawable.setLoopCount(1);
        AsyncTaskUtils.exe(new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                AddressUtils.getAddressJsonString();
                return null;
            }
        });
        mCountdownView.setTimeMillis(5000);
        mCountdownView.start();
        mCountdownView.setOnFinishListener(() ->
                next()
        );

    }


    @OnClick(R.id.countdown_view)
    public void onClick() {
        mCountdownView.setClickable(false);
        next();
    }

    private void next() {
        mCountdownView.stop();
        if (mGifDrawable != null && mGifDrawable.isPlaying()) {
            mGifDrawable.stop();
        }

        if (!Utils.getFirstEnter() && !CheckUtils.isEmpty(Utils.getLocation())) {
            Utils.startActivity(this, MainActivity.class);
        } else if (!Utils.getFirstEnter() && CheckUtils.isEmpty(Utils.getLocation())) {
            Bundle bundle = new Bundle();
            bundle.putBoolean(IntentConfig.FIRST_SELECT_SUPPLIER, true);
            Utils.startActivity(this, SelectSupplierActivity.class, bundle);
        } else {
            Utils.startActivity(this, UserGuideActivity.class);
        }
        //        if (!CheckUtils.isEmpty(Utils.getLocation())) {
        //            Utils.startActivity(this, MainActivity.class);
        //        } else {
        //            //            Utils.startActivity(this, UserGuideActivity.class);
        //            //            Bundle bundle = new Bundle();
        //            //            bundle.putBoolean(IntentConfig.FIRST_SELECT_SUPPLIER, true);
        //            //            Utils.startActivity(this, SelectSupplierActivity.class, bundle);
        //        }

        finish();
    }
}
